package com.wy.leetCode;

import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * @author HelloWorld
 * @create 2021/9/25 23:11
 * @email 154803771@qq.com
 */
public class TestWy {
    public static void main(String[] args) {
        //创建一个TreeSet集合，对新闻进行存储
        TreeSet<Journalism> list = new TreeSet<>();
        //向集合中进行添加元素
        list.add(new Journalism("孟晚舟获释:感谢祖国和人民的支持", 497));
        list.add(new Journalism("孟晚舟以不认罪方式获释令人欣慰", 483));
        list.add(new Journalism("华为声明:期待孟晚舟安全回国", 476));
        list.add(new Journalism("孟晚舟获释回国 外交部回应", 464));
        list.add(new Journalism("中国民企500强华为第一恒大缺席", 460));
        list.add(new Journalism("海航董事长陈峰总把因果挂嘴边", 441));
        list.add(new Journalism("江启臣祝贺朱立伦当选国民党主席", 433));
        list.add(new Journalism("多校已明确国庆放假如何安排", 430));
        list.add(new Journalism("孟晚舟律师:起诉书或将多月后被驳回", 414));
        list.add(new Journalism("一图读懂孟晚舟事件时间线", 401));
        list.add(new Journalism("河南一对祖孙被洪水冲走遇难", 348));
        list.add(new Journalism("平顶山一中学内涝", 395));
        list.add(new Journalism("餐饮隐性消费:啥也没点已消费几十", 386));
        list.add(new Journalism("男子复婚遭拒 拿毒针刺死前妻男友", 374));
        list.add(new Journalism("厦门国庆期间旅行社暂停组接团", 361));
        list.add(new Journalism("云南一铁皮船发生沉船事故", 357));
        //打印集合
        printTreeSet(list);
        Scanner scanner = new Scanner(System.in);
        while (true) {
            printTreeSet(list);
            System.out.println("请选择一条新闻进行操作,请输入1-16");
            //选择新闻的编号进行操作
            int index = scanner.nextInt();
            int count = 0;       //用来记录索引 找到所需要的新闻
            //用一个空对象来接收这个对象
            Journalism temp = null;
            for (Journalism journalism : list) {
                count++;
                if (index == count) {
                    //打印需要的新闻
                    System.out.println(journalism);
                    //用空对象来接收需要修改的对象
                    temp = journalism;
                    System.out.println(count + "  " + journalism.getContent() + "  " + journalism.getWeight() + "万");
                    break;
                }
            }

            // 【用流删除就可以了】
            Journalism finalTemp = temp;
            list = list.stream().filter(journalism -> journalism != finalTemp).collect(Collectors.toCollection(TreeSet::new));
            //TODO 第一次可以删除  第二次删除不掉了，之后的集合长度越来越长
            //TODO 整体思路是 删除需要被修改的对象 然后重新加入 集合自然排序
            //修改后重新添加
            Journalism choice1 = choice(temp);//修改对象的
            list.add(choice1);



        }
    }

    //将点赞 评论 收藏 加入数组中
    public static Journalism choice(Journalism journalism) {
        Scanner scanner = new Scanner(System.in);
        choiceLoop:
        while (true) {
            System.out.println("请输入需要操作，1.点赞，2.评论，3.收藏");
            String i = scanner.nextLine();
            switch (i) {
                case "1":
                    journalism.setThumbsUp(journalism.getThumbsUp() + 1);
                    System.out.println("点赞成功！");
                    break choiceLoop;
                case "2":
                    journalism.setComment(journalism.getComment() + 1);
                    System.out.println("请输入您想要评论的内容");
                    scanner.next();
                    System.out.println("感谢您的评论！");
                    break choiceLoop;
                case "3":
                    journalism.setCollection(journalism.getCollection() + 1);
                    System.out.println("收藏成功！");
                    break choiceLoop;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
        journalism.getWeight();
        return journalism;
    }

    //按照格式打印所有集合的内容
    public static void printTreeSet(TreeSet<Journalism> list) {
        int count = 0;
        for (Journalism journalism : list) {
            count++;
            System.out.println(count + "  " + journalism.getContent() + "  " + journalism.getWeight() + "万");
        }
    }

}

class Journalism implements Comparable<Journalism> {
    private String content;     //内容
    private double thumbsUp;    //点赞
    private double comment;     //评论
    private double collection;  //收藏
    private double weight;     //权重

    public Journalism() {

    }

    public Journalism(String content, double weight) {
        this.content = content;
        this.weight = weight;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


    public double getThumbsUp() {
        return thumbsUp;
    }

    public void setThumbsUp(double thumbsUp) {
        this.thumbsUp = thumbsUp;
    }

    public double getComment() {
        return comment;
    }

    public void setComment(double comment) {
        this.comment = comment;
    }

    public double getCollection() {
        return collection;
    }

    public void setCollection(double collection) {
        this.collection = collection;
    }

    //进行计算权重
    public double getWeight() {
        this.weight = weight + (thumbsUp * 0.3 + comment * 0.5 + collection * 0.8) * 10;
        return this.weight;
    }

    @Override
    public int compareTo(Journalism o) {
        int result = (int) (o.getWeight() - this.getWeight());
        result = result == 0 ? this.content.compareTo(o.content) : result;
        return result;
    }
}
